// pages/address/index.js
import { showToastUtil, phoneReg } from '../../utils/util'
import { addTakeAddress, delAddress, updateAddress } from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    linkMan: '',
    mobile: '',
    addlist: [],
    address: '',
    isDefault: false,
    addressVis: false,
    addressId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { item } = options
    const { id, linkMan, mobile, address, isDefault, provinceId, provinceStr, cityId, cityStr, districtId, areaStr } = item ? JSON.parse(item) : {}
    if (id) {
      const addlist = [
        { id: provinceId, name: provinceStr },
        { id: cityId, name: cityStr, pid: provinceId },
        { id: districtId, name: areaStr, pid: cityId },
      ]
      this.setData({ addressId: Number(id), linkMan, mobile: Number(mobile), isDefault, address, addlist })
    }
  },
  // 信息变化通用模板
  change(e) {
    const { type } = e.target.dataset
    const { value } = e.detail
    this.setData({ [type]: value })
  },
  // 级联地址弹框显隐
  addressModal() {
    this.setData({ addressVis: !this.data.addressVis })

  },
  // 所在地区
  addChange(val) {
    const { detail } = val
    this.setData({ addlist: detail, addressVis: false })
  },
  // 确认-添加收货地址
  confirmBtn() {
    const { addressId, linkMan, mobile, addlist = [], address, isDefault } = this.data
    if (!linkMan || !mobile || addlist.length === 0 || !address) {
      showToastUtil('请填写完整内容')
    } else if (!phoneReg(mobile)) {
      showToastUtil('请填写11位手机号码')
    } else {
      const params = {
        linkMan, mobile, provinceId: addlist[0].id, cityId: addlist[1].id, districtId: addlist[2].id, address, isDefault
      }
      // 添加
      if (!addressId) {
        addTakeAddress(params).then(res => {
          if (res.code === 0) {
            showToastUtil('添加成功')
            wx.navigateBack({
              delta: 1  // 返回上一级页面
            })
          } else {
            showToastUtil('添加失败')
          }
        })
      } else {
        // 修改
        const upParams = {
          id: addressId,
          ...params
        }
        updateAddress(upParams).then(res => {
          if (res.code === 0) {
            showToastUtil('修改成功')
            wx.navigateBack({
              delta: 1  // 返回上一级页面
            })
          } else {
            showToastUtil('修改失败')
          }
        })
      }
    }
  },
  // 删除-删除收货地址
  delBtn() {
    const { addressId } = this.data
    wx.showModal({
      title: '确认删除该地址？',
      content: '',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (result) => {
        if (result.confirm) {
          delAddress(addressId).then(res => {
            if (res.code === 0) {
              showToastUtil('删除成功')
              wx.navigateBack({
                delta: 1  // 返回上一级页面
              })
            } else {
              showToastUtil('删除失败')
            }
          })
        }
      }
    });


  },
})